package org.daddybod;

import org.locationtech.proj4j.*;

/**
 * Refer to https://blog.csdn.net/yuyingying1986/article/details/108626924
 */
public class CoordinateConvertor {

    public static void main(String[] args) {

        CRSFactory crsFactory = new CRSFactory();

        //源坐标系统
        final String SourceCRS = "4326";
        //params来自 http://epsg.io/4326   Export-->PROJ.4
        final String SourceCRS_params="+proj=longlat +datum=WGS84 +no_defs ";
        CoordinateReferenceSystem source = crsFactory.createFromParameters(SourceCRS, SourceCRS_params);

        //目标坐标系统
        //params来自 http://epsg.io/3857   Export-->PROJ.4
        String TargetCRS = "3857";
        String TargetCRS_params="+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs";
        CoordinateReferenceSystem  target = crsFactory.createFromParameters(TargetCRS, TargetCRS_params);

        //定义转换类WGS84转2436
        CoordinateTransformFactory ctf = new CoordinateTransformFactory();
        CoordinateTransform transform = ctf.createTransform(source, target);

        //WGS84坐标系转换
//INPUT        121.532591 31.236265

        ProjCoordinate projCoordinate = new ProjCoordinate( 121.532591, 31.236265);
        transform.transform(projCoordinate, projCoordinate);

        //projCoordinate.x 和 projCoordinate.y 就是转换结果
//OUTPUT        13528946.14    3663470.81 0.00
        System.out.println(projCoordinate.x + "," + projCoordinate.y);

    }

}
